package com.mdd.admin.controller.client;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.IClientSupplierService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.ClientSupplierCreateValidate;
import com.mdd.admin.validate.ClientSupplierUpdateValidate;
import com.mdd.admin.validate.ClientSupplierSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ClientSupplierListedVo;
import com.mdd.admin.vo.ClientSupplierDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/supplier")
@Api(tags = "供应商管理")
public class ClientSupplierController {

    @Resource
    IClientSupplierService iClientSupplierService;

    @GetMapping("/list")
    @ApiOperation(value="供应商列表")
    public AjaxResult<PageResult<ClientSupplierListedVo>> list(@Validated PageValidate pageValidate,
                                                               @Validated ClientSupplierSearchValidate searchValidate) {
        PageResult<ClientSupplierListedVo> list = iClientSupplierService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="供应商详情")
    public AjaxResult<ClientSupplierDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        ClientSupplierDetailVo detail = iClientSupplierService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "供应商新增")
    @PostMapping("/add")
    @ApiOperation(value="供应商新增")
    public AjaxResult<Object> add(@Validated @RequestBody ClientSupplierCreateValidate createValidate) {
        iClientSupplierService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "供应商编辑")
    @PostMapping("/edit")
    @ApiOperation(value="供应商编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody ClientSupplierUpdateValidate updateValidate) {
        iClientSupplierService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "供应商删除")
    @PostMapping("/del")
    @ApiOperation(value="供应商删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iClientSupplierService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
